package com.jsb.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.annotation.SystemControllerLog;
import com.jsb.service.AnswerService;
import com.jsb.service.QuestionService;
import com.jsb.service.UserDealRecordService;
import com.jsb.service.UserService;
import com.jsb.common.config.Const;
import com.jsb.common.utils.StringUtils;
import com.jsb.dto.UserDealRecordComplex;
import com.jsb.dto.UserQuestion;
import com.jsb.model.Answer;
import com.jsb.model.AnswerExample;
import com.jsb.model.Question;
import com.jsb.model.QuestionExample;
import com.jsb.model.User;
import com.jsb.model.UserDealRecord;
import com.jsb.model.UserDealRecordExample;
import com.jsb.model.UserDealRecordExample.Criteria;

@Controller
public class QuestionController{
	
	private int defaultPageSize=10;
	
	private String redirect = "redirect:/question/list.do";
	
	@Resource
	private QuestionService questionService;
	@Resource
	private UserService userService;
	@Resource
	private AnswerService answerService;
	@Resource
	private UserDealRecordService userDealRecordService;
	
	@RequestMapping("/question/list")
	public String list(
			Model model,String phone,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		QuestionExample questionExample = new QuestionExample();
		List<User> userByLikePhone = userService.getUserByLikePhone(phone);
		for (User user : userByLikePhone) {
			String userid = user.getPhoneNumber();
			com.jsb.model.QuestionExample.Criteria criteria = questionExample.createCriteria();
			if(StringUtils.isNotEmpty(userid)){
				criteria.andPhoneNumEqualTo(userid);
			}
			questionExample.or(criteria);
		}
		int totalCount = questionService.countQuestion(questionExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		questionExample.setPageIndex((pageNum - 1) * pageSize);
		questionExample.setPageSize(pageSize);
		questionExample.setOrderByClause("date desc");
		List<Question> questions = questionService.getPageQuestion(questionExample);
		for (Question question : questions) {
			String userid = question.getPhoneNum();
			User userByPhoneNumber = userService.getUserByPhoneNumber(userid);
			if(null != userByPhoneNumber){
				question.setPhoneNum(userByPhoneNumber.getPhone());
			}
		}
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("questions", questions);
		model.addAttribute("phone", phone);
		return "/Question/list";
	}
	
	@RequestMapping("/question/toAdd")
	public String toAdd(Model model) {
		return "/Question/add";
	}

	@RequestMapping("/question/add")
	public String add(Model model, @ModelAttribute("question") Question question) {
		questionService.addQuestion(question);
		return redirect;
	}

	@RequestMapping("/question/del")
	public String del(Model model,
			@RequestParam(value = "uuid", required = true) String uuid) {
		questionService.delQuestionByUuid(uuid);
		return redirect;
	}

	@RequestMapping("/question/toEdit")
	public String toEdit(Model model,
			@RequestParam(value = "uuid", required = true) String uuid) {
		Question question = questionService.getQuestionByUuid(uuid);
		model.addAttribute("question", question);
		return "/Question/edit";
	}

	@RequestMapping("/question/edit")
	@SystemControllerLog(description = "编辑悬赏")
	public String edit(Model model, @ModelAttribute("question") Question question) {
		questionService.updateQuestion(question);
		return redirect;
	}

	@RequestMapping("/question/view")
	public String view(Model model,
			@RequestParam(value = "uuid", required = true) String uuid) {
		Question question = questionService.getQuestionByUuid(uuid);
		String userid = question.getPhoneNum();
		User userByPhoneNumber = userService.getUserByPhoneNumber(userid);
		if(null != userByPhoneNumber){
			question.setPhoneNum(userByPhoneNumber.getPhone());
		}
		String answerUserPhones = question.getAnswerUserPhones();
		if(StringUtils.isNotEmpty(answerUserPhones)){
			if(answerUserPhones.contains(",")){
				String phone = "";
				String[] split = answerUserPhones.split(",");
				for (String str : split) {
					User userByPhoneNumber2 = userService.getUserByPhoneNumber(str);
					String pn = userByPhoneNumber2.getPhone();
					pn += ",";
					phone += pn;
				}
				question.setAnswerUserPhones(phone);
			}else{
				User userByPhoneNumber2 = userService.getUserByPhoneNumber(answerUserPhones);
				if(null != userByPhoneNumber2){
					question.setAnswerUserPhones(userByPhoneNumber2.getPhone());
				}
			}
			
		}
		model.addAttribute("question", question);
		return "/Question/view";
	}
	@RequestMapping("/question/viewDetail")
	public String viewDetail(Model model,
			@RequestParam(value = "uuid", required = true) String uuid) {
		Question question = questionService.getQuestionByUuid(uuid);
		UserQuestion userQuestion= new UserQuestion();
		userQuestion.setQuestion(question);
		String phoneNum = question.getPhoneNum();
		User userByPhoneNumber = userService.getUserByPhoneNumber(phoneNum);
		userQuestion.setUser(userByPhoneNumber);
		model.addAttribute("userQuestion", userQuestion);
		return "/Question/viewDetail";
	}
	@RequestMapping("/question/answerList")
	public String answerList(Model model,
		@RequestParam(value = "uuid", required = true) String uuid,
		@RequestParam(value = "pageNum", required = false) Integer pageNum,
		@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		AnswerExample example= new AnswerExample();
		example.createCriteria().andQuestionUuidEqualTo(uuid);
		int totalCount = answerService.countAnswer(example);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		example.setPageIndex((pageNum - 1) * pageSize);
		example.setPageSize(pageSize);
		example.setOrderByClause("date desc");
		List<Answer> answers = answerService.selectByExample(example);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("answers", answers);
		return "/Question/answerList";
	}
	@RequestMapping("/question/moneyDetail")
	public String moneyDetail(Model model,
			@RequestParam(value = "uuid", required = true) String uuid,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		
		UserDealRecordExample dealRecordExample= new UserDealRecordExample();
		Criteria criteria = dealRecordExample.createCriteria();
		criteria.andBusinessNoEqualTo(uuid);
		
		int totalCount = userDealRecordService.countUserDealRecord(dealRecordExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		dealRecordExample.setPageIndex((pageNum - 1) * pageSize);
		dealRecordExample.setPageSize(pageSize);
		
		dealRecordExample.setOrderByClause("createDate desc");
		List<UserDealRecord> userDealRecords = userDealRecordService.selectByExample(dealRecordExample);
		List<UserDealRecordComplex> userDealRecordComplexs= new ArrayList<UserDealRecordComplex>();
		for (UserDealRecord userDealRecord : userDealRecords) {
			UserDealRecordComplex userDealRecordComplex= new UserDealRecordComplex();
			userDealRecordComplex.setUserDealRecord(userDealRecord);
			String userId = userDealRecord.getUserId();
			User user = userService.getUserByPhoneNumber(userId);
			userDealRecordComplex.setUser(user);
			userDealRecordComplexs.add(userDealRecordComplex);
		}
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("userDealRecordComplexs", userDealRecordComplexs);
		return "/Question/moneyDetail";
	}
}
